# Copyright (c) Open Enclave SDK contributors.
# Licensed under the MIT License.

# Build the C library, using local sources and sources from musl.
set(MUSLSRC ${PROJECT_SOURCE_DIR}/3rdparty/musl/musl/src)

if (OE_SGX)
  add_enclave_library(
    oelibasm
    OBJECT
    sgx/exp2l.S
    ${MUSLSRC}/fenv/x86_64/fenv.s
    ${MUSLSRC}/math/x86_64/acosl.s
    ${MUSLSRC}/math/x86_64/asinl.s
    ${MUSLSRC}/math/x86_64/log1pl.s
    ${MUSLSRC}/math/x86_64/log2l.s
    ${MUSLSRC}/math/x86_64/logl.s
    #${MUSLSRC}/math/x86_64/exp2l.s
    ${MUSLSRC}/setjmp/x86_64/longjmp.s
    ${MUSLSRC}/setjmp/x86_64/setjmp.s
    ${MUSLSRC}/string/x86_64/memcpy.s
    ${MUSLSRC}/string/x86_64/memmove.s
    ${MUSLSRC}/string/x86_64/memset.s)
elseif (OE_TRUSTZONE)
  add_enclave_library(
    oelibasm
    OBJECT
    ${MUSLSRC}/math/aarch64/sqrt.c
    ${MUSLSRC}/math/aarch64/sqrtf.c
    ${MUSLSRC}/fenv/aarch64/fenv.s
    ${MUSLSRC}/setjmp/aarch64/longjmp.s
    ${MUSLSRC}/setjmp/aarch64/setjmp.s)
endif ()

maybe_build_using_clangw(oelibasm)

# CMake unconditionally adds `-DNDEBUG` etc. as flags even when
# assembling, and clang errors because we treat warnings as errors.
# So we move the assembly code into a fake library, turn off the
# error, and then inject the objects into the actual library.
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR USE_CLANGW)
  enclave_compile_options(oelibasm PRIVATE
                          -Wno-error=unused-command-line-argument)
endif ()

set(PLATFORM_SRC "")

if (OE_SGX)
  list(
    APPEND
    PLATFORM_SRC
    sgx/arc4random.c
    ${MUSLSRC}/math/x86_64/sqrt.c
    ${MUSLSRC}/math/x86_64/sqrtl.c
    ${MUSLSRC}/math/x86_64/sqrtf.c
    sgx/init.c)
else ()
  list(
    APPEND
    PLATFORM_SRC
    ${MUSLSRC}/math/exp2l.c
    ${MUSLSRC}/string/memcpy.c
    ${MUSLSRC}/string/memmove.c
    ${MUSLSRC}/string/memset.c
    optee/arc4random.c
    optee/init.c
    optee/trace.c)
endif ()

if (ENABLE_FUZZING)
  list(
    APPEND
    PLATFORM_SRC
    ${MUSLSRC}/stat/lstat.c
    ${MUSLSRC}/passwd/getgr_a.c
    ${MUSLSRC}/passwd/getgr_r.c
    ${MUSLSRC}/passwd/getgrent_a.c
    ${MUSLSRC}/passwd/getgrent.c
    ${MUSLSRC}/passwd/getgrouplist.c
    ${MUSLSRC}/passwd/getpw_a.c
    ${MUSLSRC}/passwd/getpw_r.c
    ${MUSLSRC}/passwd/getpwent_a.c
    ${MUSLSRC}/passwd/getpwent.c
    ${MUSLSRC}/passwd/nscd_query.c
    ${MUSLSRC}/thread/pthread_cleanup_push.c)
endif ()

add_enclave_library(
  oelibc
  STATIC
  abort.c
  atexit.c
  backtrace.c
  dladdr.c
  errno.c
  epoll.c
  exit.c
  freeaddrinfo.c
  getaddrinfo.c
  getnameinfo.c
  kill.c
  libunwind_stubs.c
  link.c
  malloc.c
  mman.c
  pthread.c
  sched_yield.c
  sigaction.c
  signal.c
  stdlib.c
  strerror.c
  syscalls.c
  regcomp.c
  regexec.c
  tre-mem.c
  __printf_chk.c
  __fprintf_chk.c
  __sprintf_chk.c
  __snprintf_chk.c
  __vprintf_chk.c
  __vfprintf_chk.c
  __vsprintf_chk.c
  __vsnprintf_chk.c
  ${MUSLSRC}/complex/cabs.c
  ${MUSLSRC}/complex/cabsf.c
  ${MUSLSRC}/complex/cabsl.c
  ${MUSLSRC}/complex/cacos.c
  ${MUSLSRC}/complex/cacosf.c
  ${MUSLSRC}/complex/cacosh.c
  ${MUSLSRC}/complex/cacoshf.c
  ${MUSLSRC}/complex/cacoshl.c
  ${MUSLSRC}/complex/cacosl.c
  ${MUSLSRC}/complex/carg.c
  ${MUSLSRC}/complex/cargf.c
  ${MUSLSRC}/complex/cargl.c
  ${MUSLSRC}/complex/casin.c
  ${MUSLSRC}/complex/casinf.c
  ${MUSLSRC}/complex/casinh.c
  ${MUSLSRC}/complex/casinhf.c
  ${MUSLSRC}/complex/casinhl.c
  ${MUSLSRC}/complex/catan.c
  ${MUSLSRC}/complex/catanf.c
  ${MUSLSRC}/complex/catanh.c
  ${MUSLSRC}/complex/catanhf.c
  ${MUSLSRC}/complex/catanhl.c
  ${MUSLSRC}/complex/catanl.c
  ${MUSLSRC}/complex/ccos.c
  ${MUSLSRC}/complex/ccosf.c
  ${MUSLSRC}/complex/ccosh.c
  ${MUSLSRC}/complex/ccoshf.c
  ${MUSLSRC}/complex/ccoshl.c
  ${MUSLSRC}/complex/ccosl.c
  ${MUSLSRC}/complex/__cexp.c
  ${MUSLSRC}/complex/cexp.c
  ${MUSLSRC}/complex/__cexpf.c
  ${MUSLSRC}/complex/cexpf.c
  ${MUSLSRC}/complex/cexpl.c
  ${MUSLSRC}/complex/cimag.c
  ${MUSLSRC}/complex/cimagf.c
  ${MUSLSRC}/complex/cimagl.c
  ${MUSLSRC}/complex/clog.c
  ${MUSLSRC}/complex/clogf.c
  ${MUSLSRC}/complex/clogl.c
  ${MUSLSRC}/complex/conj.c
  ${MUSLSRC}/complex/conjf.c
  ${MUSLSRC}/complex/conjl.c
  ${MUSLSRC}/complex/cpow.c
  ${MUSLSRC}/complex/cpowf.c
  ${MUSLSRC}/complex/cpowl.c
  ${MUSLSRC}/complex/cproj.c
  ${MUSLSRC}/complex/cprojf.c
  ${MUSLSRC}/complex/cprojl.c
  ${MUSLSRC}/complex/creal.c
  ${MUSLSRC}/complex/crealf.c
  ${MUSLSRC}/complex/creall.c
  ${MUSLSRC}/complex/csin.c
  ${MUSLSRC}/complex/csinf.c
  ${MUSLSRC}/complex/csinh.c
  ${MUSLSRC}/complex/csinhf.c
  ${MUSLSRC}/complex/csinhl.c
  ${MUSLSRC}/complex/csinl.c
  ${MUSLSRC}/complex/csqrt.c
  ${MUSLSRC}/complex/csqrtf.c
  ${MUSLSRC}/complex/csqrtl.c
  ${MUSLSRC}/complex/ctan.c
  ${MUSLSRC}/complex/ctanf.c
  ${MUSLSRC}/complex/ctanh.c
  ${MUSLSRC}/complex/ctanhf.c
  ${MUSLSRC}/complex/ctanhl.c
  ${MUSLSRC}/complex/ctanl.c
  ${MUSLSRC}/conf/confstr.c
  ${MUSLSRC}/conf/fpathconf.c
  ${MUSLSRC}/conf/legacy.c
  ${MUSLSRC}/conf/pathconf.c
  ${MUSLSRC}/conf/sysconf.c
  ${MUSLSRC}/ctype/__ctype_b_loc.c
  ${MUSLSRC}/ctype/__ctype_get_mb_cur_max.c
  ${MUSLSRC}/ctype/__ctype_tolower_loc.c
  ${MUSLSRC}/ctype/__ctype_toupper_loc.c
  ${MUSLSRC}/ctype/isalnum.c
  ${MUSLSRC}/ctype/isalpha.c
  ${MUSLSRC}/ctype/isascii.c
  ${MUSLSRC}/ctype/isblank.c
  ${MUSLSRC}/ctype/iscntrl.c
  ${MUSLSRC}/ctype/isdigit.c
  ${MUSLSRC}/ctype/isgraph.c
  ${MUSLSRC}/ctype/islower.c
  ${MUSLSRC}/ctype/isprint.c
  ${MUSLSRC}/ctype/ispunct.c
  ${MUSLSRC}/ctype/isspace.c
  ${MUSLSRC}/ctype/isupper.c
  ${MUSLSRC}/ctype/iswalnum.c
  ${MUSLSRC}/ctype/iswalpha.c
  ${MUSLSRC}/ctype/iswblank.c
  ${MUSLSRC}/ctype/iswcntrl.c
  ${MUSLSRC}/ctype/iswctype.c
  ${MUSLSRC}/ctype/iswdigit.c
  ${MUSLSRC}/ctype/iswgraph.c
  ${MUSLSRC}/ctype/iswlower.c
  ${MUSLSRC}/ctype/iswprint.c
  ${MUSLSRC}/ctype/iswpunct.c
  ${MUSLSRC}/ctype/iswspace.c
  ${MUSLSRC}/ctype/iswupper.c
  ${MUSLSRC}/ctype/iswxdigit.c
  ${MUSLSRC}/ctype/isxdigit.c
  ${MUSLSRC}/ctype/toascii.c
  ${MUSLSRC}/ctype/tolower.c
  ${MUSLSRC}/ctype/toupper.c
  ${MUSLSRC}/ctype/towctrans.c
  ${MUSLSRC}/ctype/wcswidth.c
  ${MUSLSRC}/ctype/wctrans.c
  ${MUSLSRC}/ctype/wcwidth.c
  ${MUSLSRC}/dirent/opendir.c
  ${MUSLSRC}/dirent/readdir.c
  ${MUSLSRC}/dirent/rewinddir.c
  ${MUSLSRC}/dirent/closedir.c
  ${MUSLSRC}/fcntl/open.c
  ${MUSLSRC}/fcntl/fcntl.c
  ${MUSLSRC}/env/clearenv.c
  ${MUSLSRC}/env/__environ.c
  ${MUSLSRC}/env/getenv.c
  ${MUSLSRC}/env/putenv.c
  ${MUSLSRC}/env/setenv.c
  ${MUSLSRC}/env/unsetenv.c
  ${MUSLSRC}/exit/assert.c
  ${MUSLSRC}/exit/_Exit.c
  ${MUSLSRC}/fenv/fegetexceptflag.c
  ${MUSLSRC}/fenv/feholdexcept.c
  ${MUSLSRC}/fenv/fesetexceptflag.c
  ${MUSLSRC}/fenv/fesetround.c
  ${MUSLSRC}/fenv/feupdateenv.c
  ${MUSLSRC}/fenv/__flt_rounds.c
  ${MUSLSRC}/internal/floatscan.c
  ${MUSLSRC}/internal/intscan.c
  ${MUSLSRC}/internal/libc.c
  ${MUSLSRC}/internal/procfdname.c
  ${MUSLSRC}/internal/shgetc.c
  ${MUSLSRC}/legacy/getpagesize.c
  ${MUSLSRC}/locale/bind_textdomain_codeset.c
  ${MUSLSRC}/locale/catclose.c
  ${MUSLSRC}/locale/catgets.c
  ${MUSLSRC}/locale/catopen.c
  ${MUSLSRC}/locale/c_locale.c
  ${MUSLSRC}/locale/dcngettext.c
  ${MUSLSRC}/locale/duplocale.c
  ${MUSLSRC}/locale/freelocale.c
  ${MUSLSRC}/locale/iconv.c
  ${MUSLSRC}/locale/iconv_close.c
  ${MUSLSRC}/locale/langinfo.c
  ${MUSLSRC}/locale/__lctrans.c
  ${MUSLSRC}/locale/localeconv.c
  ${MUSLSRC}/locale/locale_map.c
  ${MUSLSRC}/locale/__mo_lookup.c
  ${MUSLSRC}/locale/newlocale.c
  ${MUSLSRC}/locale/pleval.c
  ${MUSLSRC}/locale/setlocale.c
  ${MUSLSRC}/locale/strcoll.c
  ${MUSLSRC}/locale/strfmon.c
  ${MUSLSRC}/locale/strxfrm.c
  ${MUSLSRC}/locale/textdomain.c
  ${MUSLSRC}/locale/uselocale.c
  ${MUSLSRC}/locale/wcscoll.c
  ${MUSLSRC}/locale/wcsxfrm.c
  ${MUSLSRC}/linux/epoll.c
  ${MUSLSRC}/linux/flock.c
  ${MUSLSRC}/linux/getrandom.c
  ${MUSLSRC}/linux/mount.c
  ${MUSLSRC}/linux/sysinfo.c
  ${MUSLSRC}/math/__math_divzero.c
  ${MUSLSRC}/math/__math_divzerof.c
  ${MUSLSRC}/math/__math_invalid.c
  ${MUSLSRC}/math/__math_invalidf.c
  ${MUSLSRC}/math/__math_oflow.c
  ${MUSLSRC}/math/__math_oflowf.c
  ${MUSLSRC}/math/__math_uflow.c
  ${MUSLSRC}/math/__math_uflowf.c
  ${MUSLSRC}/math/__math_xflow.c
  ${MUSLSRC}/math/__math_xflowf.c
  ${MUSLSRC}/math/acos.c
  ${MUSLSRC}/math/acosf.c
  ${MUSLSRC}/math/acosh.c
  ${MUSLSRC}/math/acoshf.c
  ${MUSLSRC}/math/acoshl.c
  ${MUSLSRC}/math/asin.c
  ${MUSLSRC}/math/asinf.c
  ${MUSLSRC}/math/asinh.c
  ${MUSLSRC}/math/asinhf.c
  ${MUSLSRC}/math/asinhl.c
  ${MUSLSRC}/math/atan2.c
  ${MUSLSRC}/math/atan2f.c
  ${MUSLSRC}/math/atan2l.c
  ${MUSLSRC}/math/atan.c
  ${MUSLSRC}/math/atanf.c
  ${MUSLSRC}/math/atanh.c
  ${MUSLSRC}/math/atanhf.c
  ${MUSLSRC}/math/atanhl.c
  ${MUSLSRC}/math/atanl.c
  ${MUSLSRC}/math/cbrt.c
  ${MUSLSRC}/math/cbrtf.c
  ${MUSLSRC}/math/cbrtl.c
  ${MUSLSRC}/math/ceil.c
  ${MUSLSRC}/math/ceilf.c
  ${MUSLSRC}/math/ceill.c
  ${MUSLSRC}/math/copysign.c
  ${MUSLSRC}/math/copysignf.c
  ${MUSLSRC}/math/copysignl.c
  ${MUSLSRC}/math/__cos.c
  ${MUSLSRC}/math/cos.c
  ${MUSLSRC}/math/__cosdf.c
  ${MUSLSRC}/math/cosf.c
  ${MUSLSRC}/math/cosh.c
  ${MUSLSRC}/math/coshf.c
  ${MUSLSRC}/math/coshl.c
  ${MUSLSRC}/math/__cosl.c
  ${MUSLSRC}/math/cosl.c
  ${MUSLSRC}/math/erf.c
  ${MUSLSRC}/math/erff.c
  ${MUSLSRC}/math/erfl.c
  ${MUSLSRC}/math/exp_data.c
  ${MUSLSRC}/math/exp10.c
  ${MUSLSRC}/math/exp10f.c
  ${MUSLSRC}/math/exp10l.c
  ${MUSLSRC}/math/exp2.c
  ${MUSLSRC}/math/exp2f.c
  ${MUSLSRC}/math/exp2f_data.c
  ${MUSLSRC}/math/exp.c
  ${MUSLSRC}/math/expf.c
  ${MUSLSRC}/math/expl.c
  ${MUSLSRC}/math/expm1.c
  ${MUSLSRC}/math/expm1f.c
  ${MUSLSRC}/math/expm1l.c
  ${MUSLSRC}/math/__expo2.c
  ${MUSLSRC}/math/__expo2f.c
  ${MUSLSRC}/math/fabs.c
  ${MUSLSRC}/math/fabsf.c
  ${MUSLSRC}/math/fabsl.c
  ${MUSLSRC}/math/fdim.c
  ${MUSLSRC}/math/fdimf.c
  ${MUSLSRC}/math/fdiml.c
  ${MUSLSRC}/math/finite.c
  ${MUSLSRC}/math/finitef.c
  ${MUSLSRC}/math/floor.c
  ${MUSLSRC}/math/floorf.c
  ${MUSLSRC}/math/floorl.c
  ${MUSLSRC}/math/fma.c
  ${MUSLSRC}/math/fmaf.c
  ${MUSLSRC}/math/fmal.c
  ${MUSLSRC}/math/fmax.c
  ${MUSLSRC}/math/fmaxf.c
  ${MUSLSRC}/math/fmaxl.c
  ${MUSLSRC}/math/fmin.c
  ${MUSLSRC}/math/fminf.c
  ${MUSLSRC}/math/fminl.c
  ${MUSLSRC}/math/fmod.c
  ${MUSLSRC}/math/fmodf.c
  ${MUSLSRC}/math/fmodl.c
  ${MUSLSRC}/math/__fpclassify.c
  ${MUSLSRC}/math/__fpclassifyf.c
  ${MUSLSRC}/math/__fpclassifyl.c
  ${MUSLSRC}/math/frexp.c
  ${MUSLSRC}/math/frexpf.c
  ${MUSLSRC}/math/frexpl.c
  ${MUSLSRC}/math/hypot.c
  ${MUSLSRC}/math/hypotf.c
  ${MUSLSRC}/math/hypotl.c
  ${MUSLSRC}/math/ilogb.c
  ${MUSLSRC}/math/ilogbf.c
  ${MUSLSRC}/math/ilogbl.c
  ${MUSLSRC}/math/__invtrigl.c
  ${MUSLSRC}/math/j0.c
  ${MUSLSRC}/math/j0f.c
  ${MUSLSRC}/math/j1.c
  ${MUSLSRC}/math/j1f.c
  ${MUSLSRC}/math/jn.c
  ${MUSLSRC}/math/jnf.c
  ${MUSLSRC}/math/ldexp.c
  ${MUSLSRC}/math/ldexpf.c
  ${MUSLSRC}/math/ldexpl.c
  ${MUSLSRC}/math/lgamma.c
  ${MUSLSRC}/math/lgammaf.c
  ${MUSLSRC}/math/lgammaf_r.c
  ${MUSLSRC}/math/lgammal.c
  ${MUSLSRC}/math/lgamma_r.c
  ${MUSLSRC}/math/llrint.c
  ${MUSLSRC}/math/llrintf.c
  ${MUSLSRC}/math/llrintl.c
  ${MUSLSRC}/math/llround.c
  ${MUSLSRC}/math/llroundf.c
  ${MUSLSRC}/math/llroundl.c
  ${MUSLSRC}/math/log10.c
  ${MUSLSRC}/math/log10f.c
  ${MUSLSRC}/math/log10l.c
  ${MUSLSRC}/math/log1p.c
  ${MUSLSRC}/math/log1pf.c
  #${MUSLSRC}/math/log1pl.c
  ${MUSLSRC}/math/log2.c
  ${MUSLSRC}/math/log2_data.c
  ${MUSLSRC}/math/log2f.c
  ${MUSLSRC}/math/log2f_data.c
  #${MUSLSRC}/math/log2l.c
  ${MUSLSRC}/math/logb.c
  ${MUSLSRC}/math/logbf.c
  ${MUSLSRC}/math/logbl.c
  ${MUSLSRC}/math/log.c
  ${MUSLSRC}/math/log_data.c
  ${MUSLSRC}/math/logf.c
  ${MUSLSRC}/math/logf_data.c
  #${MUSLSRC}/math/logl.c
  ${MUSLSRC}/math/lrint.c
  ${MUSLSRC}/math/lrintf.c
  ${MUSLSRC}/math/lrintl.c
  ${MUSLSRC}/math/lround.c
  ${MUSLSRC}/math/lroundf.c
  ${MUSLSRC}/math/lroundl.c
  ${MUSLSRC}/math/modf.c
  ${MUSLSRC}/math/modff.c
  ${MUSLSRC}/math/modfl.c
  ${MUSLSRC}/math/nan.c
  ${MUSLSRC}/math/nanf.c
  ${MUSLSRC}/math/nanl.c
  ${MUSLSRC}/math/nearbyint.c
  ${MUSLSRC}/math/nearbyintf.c
  ${MUSLSRC}/math/nearbyintl.c
  ${MUSLSRC}/math/nextafter.c
  ${MUSLSRC}/math/nextafterf.c
  ${MUSLSRC}/math/nextafterl.c
  ${MUSLSRC}/math/nexttoward.c
  ${MUSLSRC}/math/nexttowardf.c
  ${MUSLSRC}/math/nexttowardl.c
  ${MUSLSRC}/math/__polevll.c
  ${MUSLSRC}/math/pow.c
  ${MUSLSRC}/math/pow_data.c
  ${MUSLSRC}/math/powf.c
  ${MUSLSRC}/math/powf_data.c
  ${MUSLSRC}/math/powl.c
  ${MUSLSRC}/math/remainder.c
  ${MUSLSRC}/math/remainderf.c
  ${MUSLSRC}/math/remainderl.c
  ${MUSLSRC}/math/__rem_pio2.c
  ${MUSLSRC}/math/__rem_pio2f.c
  ${MUSLSRC}/math/__rem_pio2_large.c
  ${MUSLSRC}/math/__rem_pio2l.c
  ${MUSLSRC}/math/remquo.c
  ${MUSLSRC}/math/remquof.c
  ${MUSLSRC}/math/remquol.c
  ${MUSLSRC}/math/rint.c
  ${MUSLSRC}/math/rintf.c
  ${MUSLSRC}/math/rintl.c
  ${MUSLSRC}/math/round.c
  ${MUSLSRC}/math/roundf.c
  ${MUSLSRC}/math/roundl.c
  ${MUSLSRC}/math/scalb.c
  ${MUSLSRC}/math/scalbf.c
  ${MUSLSRC}/math/scalbln.c
  ${MUSLSRC}/math/scalblnf.c
  ${MUSLSRC}/math/scalblnl.c
  ${MUSLSRC}/math/scalbn.c
  ${MUSLSRC}/math/scalbnf.c
  ${MUSLSRC}/math/scalbnl.c
  ${MUSLSRC}/math/__signbit.c
  ${MUSLSRC}/math/__signbitf.c
  ${MUSLSRC}/math/__signbitl.c
  ${MUSLSRC}/math/signgam.c
  ${MUSLSRC}/math/significand.c
  ${MUSLSRC}/math/significandf.c
  ${MUSLSRC}/math/__sin.c
  ${MUSLSRC}/math/sin.c
  ${MUSLSRC}/math/sincos.c
  ${MUSLSRC}/math/sincosf.c
  ${MUSLSRC}/math/sincosl.c
  ${MUSLSRC}/math/__sindf.c
  ${MUSLSRC}/math/sinf.c
  ${MUSLSRC}/math/sinh.c
  ${MUSLSRC}/math/sinhf.c
  ${MUSLSRC}/math/sinhl.c
  ${MUSLSRC}/math/__sinl.c
  ${MUSLSRC}/math/sinl.c
  ${MUSLSRC}/math/__tan.c
  ${MUSLSRC}/math/tan.c
  ${MUSLSRC}/math/__tandf.c
  ${MUSLSRC}/math/tanf.c
  ${MUSLSRC}/math/tanh.c
  ${MUSLSRC}/math/tanhf.c
  ${MUSLSRC}/math/tanhl.c
  ${MUSLSRC}/math/__tanl.c
  ${MUSLSRC}/math/tanl.c
  ${MUSLSRC}/math/tgamma.c
  ${MUSLSRC}/math/tgammaf.c
  ${MUSLSRC}/math/tgammal.c
  ${MUSLSRC}/math/trunc.c
  ${MUSLSRC}/math/truncf.c
  ${MUSLSRC}/math/truncl.c
  ${MUSLSRC}/misc/a64l.c
  ${MUSLSRC}/misc/basename.c
  ${MUSLSRC}/misc/dirname.c
  ${MUSLSRC}/misc/ffs.c
  ${MUSLSRC}/misc/ffsl.c
  ${MUSLSRC}/misc/ffsll.c
  ${MUSLSRC}/misc/get_current_dir_name.c
  ${MUSLSRC}/misc/getauxval.c
  ${MUSLSRC}/misc/getdomainname.c
  ${MUSLSRC}/misc/gethostid.c
  ${MUSLSRC}/misc/getopt.c
  ${MUSLSRC}/misc/getopt_long.c
  ${MUSLSRC}/misc/getrlimit.c
  ${MUSLSRC}/misc/getsubopt.c
  ${MUSLSRC}/misc/issetugid.c
  ${MUSLSRC}/misc/ioctl.c
  ${MUSLSRC}/misc/mntent.c
  ${MUSLSRC}/misc/nftw.c
  ${MUSLSRC}/misc/uname.c
  ${MUSLSRC}/misc/wordexp.c
  ${MUSLSRC}/multibyte/btowc.c
  ${MUSLSRC}/multibyte/c16rtomb.c
  ${MUSLSRC}/multibyte/c32rtomb.c
  ${MUSLSRC}/multibyte/internal.c
  ${MUSLSRC}/multibyte/mblen.c
  ${MUSLSRC}/multibyte/mbrlen.c
  ${MUSLSRC}/multibyte/mbrtoc16.c
  ${MUSLSRC}/multibyte/mbrtoc32.c
  ${MUSLSRC}/multibyte/mbrtowc.c
  ${MUSLSRC}/multibyte/mbsinit.c
  ${MUSLSRC}/multibyte/mbsnrtowcs.c
  ${MUSLSRC}/multibyte/mbsrtowcs.c
  ${MUSLSRC}/multibyte/mbstowcs.c
  ${MUSLSRC}/multibyte/mbtowc.c
  ${MUSLSRC}/multibyte/wcrtomb.c
  ${MUSLSRC}/multibyte/wcsnrtombs.c
  ${MUSLSRC}/multibyte/wcsrtombs.c
  ${MUSLSRC}/multibyte/wcstombs.c
  ${MUSLSRC}/multibyte/wctob.c
  ${MUSLSRC}/multibyte/wctomb.c
  ${MUSLSRC}/network/getpeername.c
  ${MUSLSRC}/network/getsockname.c
  ${MUSLSRC}/network/getsockopt.c
  ${MUSLSRC}/network/ntohl.c
  ${MUSLSRC}/network/ntohs.c
  ${MUSLSRC}/network/htonl.c
  ${MUSLSRC}/network/htons.c
  ${MUSLSRC}/network/inet_ntop.c
  ${MUSLSRC}/network/inet_addr.c
  ${MUSLSRC}/network/inet_aton.c
  ${MUSLSRC}/network/inet_ntoa.c
  ${MUSLSRC}/network/inet_pton.c
  ${MUSLSRC}/network/socket.c
  ${MUSLSRC}/network/socketpair.c
  ${MUSLSRC}/network/setsockopt.c
  ${MUSLSRC}/network/getsockopt.c
  ${MUSLSRC}/network/gai_strerror.c
  ${MUSLSRC}/network/gethostbyaddr.c
  ${MUSLSRC}/network/gethostbyaddr_r.c
  ${MUSLSRC}/network/gethostbyname2.c
  ${MUSLSRC}/network/gethostbyname2_r.c
  ${MUSLSRC}/network/gethostbyname.c
  ${MUSLSRC}/network/gethostbyname_r.c
  ${MUSLSRC}/network/accept.c
  ${MUSLSRC}/network/bind.c
  ${MUSLSRC}/network/inet_pton.c
  ${MUSLSRC}/network/listen.c
  ${MUSLSRC}/network/connect.c
  ${MUSLSRC}/network/shutdown.c
  ${MUSLSRC}/network/send.c
  ${MUSLSRC}/network/sendto.c
  ${MUSLSRC}/network/sendmsg.c
  ${MUSLSRC}/network/recv.c
  ${MUSLSRC}/network/recvfrom.c
  ${MUSLSRC}/network/recvmsg.c
  ${MUSLSRC}/network/res_msend.c
  ${MUSLSRC}/network/res_mkquery.c
  ${MUSLSRC}/network/if_nametoindex.c
  ${MUSLSRC}/network/dn_expand.c
  ${MUSLSRC}/network/dns_parse.c
  ${MUSLSRC}/network/res_query.c
  ${MUSLSRC}/network/resolvconf.c
  ${MUSLSRC}/network/lookup_ipliteral.c
  ${MUSLSRC}/network/res_msend.c
  ${MUSLSRC}/network/lookup_serv.c
  ${MUSLSRC}/network/lookup_name.c
  ${MUSLSRC}/prng/drand48.c
  ${MUSLSRC}/prng/lcong48.c
  ${MUSLSRC}/prng/lrand48.c
  ${MUSLSRC}/prng/mrand48.c
  ${MUSLSRC}/prng/__rand48_step.c
  ${MUSLSRC}/prng/rand.c
  ${MUSLSRC}/prng/random.c
  ${MUSLSRC}/prng/rand_r.c
  ${MUSLSRC}/prng/__seed48.c
  ${MUSLSRC}/prng/seed48.c
  ${MUSLSRC}/prng/srand48.c
  ${MUSLSRC}/regex/fnmatch.c
  ${MUSLSRC}/regex/glob.c
  #${MUSLSRC}/regex/regcomp.c
  ${MUSLSRC}/regex/regerror.c
  #${MUSLSRC}/regex/regexec.c
  #${MUSLSRC}/regex/tre-mem.c
  ${MUSLSRC}/search/hsearch.c
  ${MUSLSRC}/search/insque.c
  ${MUSLSRC}/search/lsearch.c
  ${MUSLSRC}/select/select.c
  ${MUSLSRC}/select/poll.c
  ${MUSLSRC}/search/hsearch.c
  ${MUSLSRC}/search/insque.c
  ${MUSLSRC}/search/lsearch.c
  ${MUSLSRC}/search/tdelete.c
  ${MUSLSRC}/search/tdestroy.c
  ${MUSLSRC}/search/tfind.c
  ${MUSLSRC}/search/tsearch.c
  ${MUSLSRC}/search/twalk.c
  ${MUSLSRC}/stat/fstat.c
  ${MUSLSRC}/stat/fstatat.c
  ${MUSLSRC}/stat/mkdir.c
  ${MUSLSRC}/stat/stat.c
  ${MUSLSRC}/stdio/asprintf.c
  ${MUSLSRC}/stdio/clearerr.c
  ${MUSLSRC}/stdio/dprintf.c
  ${MUSLSRC}/stdio/ext2.c
  ${MUSLSRC}/stdio/ext.c
  ${MUSLSRC}/stdio/fclose.c
  ${MUSLSRC}/stdio/__fclose_ca.c
  ${MUSLSRC}/stdio/__fdopen.c
  ${MUSLSRC}/stdio/feof.c
  ${MUSLSRC}/stdio/ferror.c
  ${MUSLSRC}/stdio/fflush.c
  ${MUSLSRC}/stdio/fgetc.c
  ${MUSLSRC}/stdio/fgetln.c
  ${MUSLSRC}/stdio/fgetpos.c
  ${MUSLSRC}/stdio/fgets.c
  ${MUSLSRC}/stdio/fgetwc.c
  ${MUSLSRC}/stdio/fgetws.c
  ${MUSLSRC}/stdio/fileno.c
  ${MUSLSRC}/stdio/flockfile.c
  ${MUSLSRC}/stdio/fmemopen.c
  ${MUSLSRC}/stdio/__fmodeflags.c
  ${MUSLSRC}/stdio/fopen.c
  ${MUSLSRC}/stdio/fopencookie.c
  ${MUSLSRC}/stdio/__fopen_rb_ca.c
  ${MUSLSRC}/stdio/fprintf.c
  ${MUSLSRC}/stdio/fputc.c
  ${MUSLSRC}/stdio/fputs.c
  ${MUSLSRC}/stdio/fputwc.c
  ${MUSLSRC}/stdio/fputws.c
  ${MUSLSRC}/stdio/fread.c
  ${MUSLSRC}/stdio/freopen.c
  ${MUSLSRC}/stdio/fscanf.c
  ${MUSLSRC}/stdio/fseek.c
  ${MUSLSRC}/stdio/fsetpos.c
  ${MUSLSRC}/stdio/ftell.c
  ${MUSLSRC}/stdio/ftrylockfile.c
  ${MUSLSRC}/stdio/funlockfile.c
  ${MUSLSRC}/stdio/fwide.c
  ${MUSLSRC}/stdio/fwprintf.c
  ${MUSLSRC}/stdio/fwrite.c
  ${MUSLSRC}/stdio/fwscanf.c
  ${MUSLSRC}/stdio/getc.c
  ${MUSLSRC}/stdio/getchar.c
  ${MUSLSRC}/stdio/getchar_unlocked.c
  ${MUSLSRC}/stdio/getc_unlocked.c
  ${MUSLSRC}/stdio/getdelim.c
  ${MUSLSRC}/stdio/getline.c
  ${MUSLSRC}/stdio/gets.c
  ${MUSLSRC}/stdio/getw.c
  ${MUSLSRC}/stdio/getwc.c
  ${MUSLSRC}/stdio/getwchar.c
  ${MUSLSRC}/stdio/__lockfile.c
  ${MUSLSRC}/stdio/ofl_add.c
  ${MUSLSRC}/stdio/ofl.c
  ${MUSLSRC}/stdio/open_memstream.c
  ${MUSLSRC}/stdio/open_wmemstream.c
  ${MUSLSRC}/stdio/__overflow.c
  ${MUSLSRC}/stdio/pclose.c
  ${MUSLSRC}/stdio/perror.c
  ${MUSLSRC}/stdio/printf.c
  ${MUSLSRC}/stdio/putc.c
  ${MUSLSRC}/stdio/putchar.c
  ${MUSLSRC}/stdio/putchar_unlocked.c
  ${MUSLSRC}/stdio/putc_unlocked.c
  ${MUSLSRC}/stdio/puts.c
  ${MUSLSRC}/stdio/putw.c
  ${MUSLSRC}/stdio/putwc.c
  ${MUSLSRC}/stdio/putwchar.c
  ${MUSLSRC}/stdio/remove.c
  ${MUSLSRC}/stdio/rename.c
  ${MUSLSRC}/stdio/rewind.c
  ${MUSLSRC}/stdio/scanf.c
  ${MUSLSRC}/stdio/setbuf.c
  ${MUSLSRC}/stdio/setbuffer.c
  ${MUSLSRC}/stdio/setlinebuf.c
  ${MUSLSRC}/stdio/setvbuf.c
  ${MUSLSRC}/stdio/snprintf.c
  ${MUSLSRC}/stdio/sprintf.c
  ${MUSLSRC}/stdio/sscanf.c
  ${MUSLSRC}/stdio/stderr.c
  ${MUSLSRC}/stdio/stdin.c
  ${MUSLSRC}/stdio/__stdio_close.c
  ${MUSLSRC}/stdio/__stdio_exit.c
  ${MUSLSRC}/stdio/__stdio_read.c
  ${MUSLSRC}/stdio/__stdio_seek.c
  ${MUSLSRC}/stdio/__stdio_write.c
  ${MUSLSRC}/stdio/stdout.c
  ${MUSLSRC}/stdio/__stdout_write.c
  ${MUSLSRC}/stdio/swprintf.c
  ${MUSLSRC}/stdio/swscanf.c
  ${MUSLSRC}/stdio/tempnam.c
  ${MUSLSRC}/stdio/tmpfile.c
  ${MUSLSRC}/stdio/__toread.c
  ${MUSLSRC}/stdio/__towrite.c
  ${MUSLSRC}/stdio/__uflow.c
  ${MUSLSRC}/stdio/ungetc.c
  ${MUSLSRC}/stdio/ungetwc.c
  ${MUSLSRC}/stdio/vasprintf.c
  ${MUSLSRC}/stdio/vdprintf.c
  ${MUSLSRC}/stdio/vfprintf.c
  ${MUSLSRC}/stdio/vfscanf.c
  ${MUSLSRC}/stdio/vfwprintf.c
  ${MUSLSRC}/stdio/vfwscanf.c
  ${MUSLSRC}/stdio/vprintf.c
  ${MUSLSRC}/stdio/vscanf.c
  ${MUSLSRC}/stdio/vsnprintf.c
  ${MUSLSRC}/stdio/vsprintf.c
  ${MUSLSRC}/stdio/vsscanf.c
  ${MUSLSRC}/stdio/vswprintf.c
  ${MUSLSRC}/stdio/vswscanf.c
  ${MUSLSRC}/stdio/vwprintf.c
  ${MUSLSRC}/stdio/vwscanf.c
  ${MUSLSRC}/stdio/wprintf.c
  ${MUSLSRC}/stdio/wscanf.c
  ${MUSLSRC}/stdlib/abs.c
  ${MUSLSRC}/stdlib/atof.c
  ${MUSLSRC}/stdlib/atoi.c
  ${MUSLSRC}/stdlib/atol.c
  ${MUSLSRC}/stdlib/atoll.c
  ${MUSLSRC}/stdlib/bsearch.c
  ${MUSLSRC}/stdlib/div.c
  ${MUSLSRC}/stdlib/ecvt.c
  ${MUSLSRC}/stdlib/fcvt.c
  ${MUSLSRC}/stdlib/gcvt.c
  ${MUSLSRC}/stdlib/imaxabs.c
  ${MUSLSRC}/stdlib/imaxdiv.c
  ${MUSLSRC}/stdlib/labs.c
  ${MUSLSRC}/stdlib/ldiv.c
  ${MUSLSRC}/stdlib/llabs.c
  ${MUSLSRC}/stdlib/lldiv.c
  ${MUSLSRC}/stdlib/qsort.c
  ${MUSLSRC}/stdlib/strtod.c
  ${MUSLSRC}/stdlib/strtol.c
  ${MUSLSRC}/stdlib/wcstod.c
  ${MUSLSRC}/stdlib/wcstol.c
  ${MUSLSRC}/string/bcmp.c
  ${MUSLSRC}/string/bcopy.c
  ${MUSLSRC}/string/bzero.c
  ${MUSLSRC}/string/explicit_bzero.c
  ${MUSLSRC}/string/index.c
  ${MUSLSRC}/string/memccpy.c
  ${MUSLSRC}/string/memchr.c
  ${MUSLSRC}/string/memcmp.c
  ${MUSLSRC}/string/memmem.c
  ${MUSLSRC}/string/mempcpy.c
  ${MUSLSRC}/string/memrchr.c
  ${MUSLSRC}/string/rindex.c
  ${MUSLSRC}/string/stpcpy.c
  ${MUSLSRC}/string/stpncpy.c
  ${MUSLSRC}/string/strcasecmp.c
  ${MUSLSRC}/string/strcasestr.c
  ${MUSLSRC}/string/strcat.c
  ${MUSLSRC}/string/strchr.c
  ${MUSLSRC}/string/strchrnul.c
  ${MUSLSRC}/string/strcmp.c
  ${MUSLSRC}/string/strcpy.c
  ${MUSLSRC}/string/strcspn.c
  ${MUSLSRC}/string/strdup.c
  ${MUSLSRC}/string/strerror_r.c
  ${MUSLSRC}/string/strlcat.c
  ${MUSLSRC}/string/strlcpy.c
  ${MUSLSRC}/string/strlen.c
  ${MUSLSRC}/string/strncasecmp.c
  ${MUSLSRC}/string/strncat.c
  ${MUSLSRC}/string/strncmp.c
  ${MUSLSRC}/string/strncpy.c
  ${MUSLSRC}/string/strndup.c
  ${MUSLSRC}/string/strnlen.c
  ${MUSLSRC}/string/strpbrk.c
  ${MUSLSRC}/string/strrchr.c
  ${MUSLSRC}/string/strsep.c
  ${MUSLSRC}/string/strsignal.c
  ${MUSLSRC}/string/strspn.c
  ${MUSLSRC}/string/strstr.c
  ${MUSLSRC}/string/strtok.c
  ${MUSLSRC}/string/strtok_r.c
  ${MUSLSRC}/string/strverscmp.c
  ${MUSLSRC}/string/swab.c
  ${MUSLSRC}/string/wcpcpy.c
  ${MUSLSRC}/string/wcpncpy.c
  ${MUSLSRC}/string/wcscasecmp.c
  ${MUSLSRC}/string/wcscasecmp_l.c
  ${MUSLSRC}/string/wcscat.c
  ${MUSLSRC}/string/wcschr.c
  ${MUSLSRC}/string/wcscmp.c
  ${MUSLSRC}/string/wcscpy.c
  ${MUSLSRC}/string/wcscspn.c
  ${MUSLSRC}/string/wcsdup.c
  ${MUSLSRC}/string/wcslen.c
  ${MUSLSRC}/string/wcsncasecmp.c
  ${MUSLSRC}/string/wcsncasecmp_l.c
  ${MUSLSRC}/string/wcsncat.c
  ${MUSLSRC}/string/wcsncmp.c
  ${MUSLSRC}/string/wcsncpy.c
  ${MUSLSRC}/string/wcsnlen.c
  ${MUSLSRC}/string/wcspbrk.c
  ${MUSLSRC}/string/wcsrchr.c
  ${MUSLSRC}/string/wcsspn.c
  ${MUSLSRC}/string/wcsstr.c
  ${MUSLSRC}/string/wcstok.c
  ${MUSLSRC}/string/wcswcs.c
  ${MUSLSRC}/string/wmemchr.c
  ${MUSLSRC}/string/wmemcmp.c
  ${MUSLSRC}/string/wmemcpy.c
  ${MUSLSRC}/string/wmemmove.c
  ${MUSLSRC}/string/wmemset.c
  ${MUSLSRC}/temp/mkdtemp.c
  ${MUSLSRC}/temp/mkostemp.c
  ${MUSLSRC}/temp/mkostemps.c
  ${MUSLSRC}/temp/mkstemp.c
  ${MUSLSRC}/temp/mkstemps.c
  ${MUSLSRC}/temp/mktemp.c
  ${MUSLSRC}/temp/__randname.c
  ${MUSLSRC}/thread/__lock.c
  ${MUSLSRC}/thread/__wait.c
  ${MUSLSRC}/time/asctime.c
  ${MUSLSRC}/time/asctime_r.c
  ${MUSLSRC}/time/clock_gettime.c
  ${MUSLSRC}/time/clock_nanosleep.c
  ${MUSLSRC}/time/ctime.c
  ${MUSLSRC}/time/ctime_r.c
  ${MUSLSRC}/time/difftime.c
  ${MUSLSRC}/time/ftime.c
  ${MUSLSRC}/time/gettimeofday.c
  ${MUSLSRC}/time/gmtime.c
  ${MUSLSRC}/time/gmtime_r.c
  ${MUSLSRC}/time/localtime_r.c
  ${MUSLSRC}/time/localtime.c
  ${MUSLSRC}/time/__map_file.c
  ${MUSLSRC}/time/mktime.c
  ${MUSLSRC}/time/__month_to_secs.c
  ${MUSLSRC}/time/nanosleep.c
  ${MUSLSRC}/time/__secs_to_tm.c
  ${MUSLSRC}/time/strftime.c
  ${MUSLSRC}/time/strptime.c
  ${MUSLSRC}/time/time.c
  ${MUSLSRC}/time/timegm.c
  ${MUSLSRC}/time/timespec_get.c
  ${MUSLSRC}/time/__tm_to_secs.c
  ${MUSLSRC}/time/__tz.c
  ${MUSLSRC}/time/wcsftime.c
  ${MUSLSRC}/time/__year_to_secs.c
  ${MUSLSRC}/unistd/access.c
  ${MUSLSRC}/unistd/chdir.c
  ${MUSLSRC}/unistd/close.c
  ${MUSLSRC}/unistd/fdatasync.c
  ${MUSLSRC}/unistd/fsync.c
  ${MUSLSRC}/unistd/ftruncate.c
  ${MUSLSRC}/unistd/getgid.c
  ${MUSLSRC}/unistd/getegid.c
  ${MUSLSRC}/unistd/getuid.c
  ${MUSLSRC}/unistd/geteuid.c
  ${MUSLSRC}/unistd/getpid.c
  ${MUSLSRC}/unistd/getppid.c
  ${MUSLSRC}/unistd/getpgrp.c
  ${MUSLSRC}/unistd/getgroups.c
  ${MUSLSRC}/unistd/dup.c
  ${MUSLSRC}/unistd/dup2.c
  ${MUSLSRC}/unistd/dup3.c
  ${MUSLSRC}/unistd/getcwd.c
  ${MUSLSRC}/unistd/gethostname.c
  ${MUSLSRC}/unistd/link.c
  ${MUSLSRC}/unistd/lseek.c
  ${MUSLSRC}/unistd/pread.c
  ${MUSLSRC}/unistd/pwrite.c
  ${MUSLSRC}/unistd/read.c
  ${MUSLSRC}/unistd/readv.c
  ${MUSLSRC}/unistd/rmdir.c
  ${MUSLSRC}/unistd/sleep.c
  ${MUSLSRC}/unistd/truncate.c
  ${MUSLSRC}/unistd/unlink.c
  ${MUSLSRC}/unistd/write.c
  ${MUSLSRC}/unistd/writev.c
  ${MUSLSRC}/unistd/usleep.c
  ${PLATFORM_SRC})

maybe_build_using_clangw(oelibc)

# NOTE: This is the minimum required for musl sources.
set_property(TARGET oelibc PROPERTY C_STANDARD 99)

enclave_compile_options(oelibc PRIVATE -fno-builtin-memcpy -fno-builtin-memset
                        -ffreestanding)

if (OE_SGX)
  enclave_compile_options(oelibc PRIVATE -mrdrnd)
endif ()

if (CMAKE_C_COMPILER_ID MATCHES GNU)
  enclave_compile_options(oelibc PRIVATE -fexcess-precision=standard
                          -frounding-math)
endif ()

# Ignore warnings from musl sources. First, shared warnings:
if (CMAKE_C_COMPILER_ID MATCHES GNU
    OR CMAKE_C_COMPILER_ID MATCHES Clang
    OR USE_CLANGW)
  enclave_compile_options(
    oelibc
    PRIVATE
    -Wno-missing-braces
    -Wno-parentheses
    -Wno-unknown-pragmas
    -Wno-conversion
    -Wno-unused-parameter
    -Wno-sign-compare)
endif ()

# Second: compiler-specific warnings:
if (CMAKE_C_COMPILER_ID MATCHES GNU)
  enclave_compile_options(
    oelibc
    PRIVATE
    -Wno-jump-misses-init
    -Wno-maybe-uninitialized
    -Wno-unknown-pragmas
    -Wno-unused-but-set-variable
    -Wno-unused-function
    -Wno-unused-value
    -Wno-unused-variable)
elseif (CMAKE_C_COMPILER_ID MATCHES Clang OR USE_CLANGW)
  enclave_compile_options(
    oelibc
    PRIVATE
    -Wno-dangling-else
    -Wno-ignored-attributes
    -Wno-logical-op-parentheses
    -Wno-string-plus-int
    -Wno-typedef-redefinition
    -Wno-unneeded-internal-declaration)
endif ()

# Disable optimizations for twalk.c to avoid Clang speculative load hardening flag compiler bug, see #2556
# Always dose this on Windows as we cannot know the version of clang.
if (WIN32 OR CMAKE_C_COMPILER_ID MATCHES Clang)
  set_source_files_properties(${MUSLSRC}/search/twalk.c PROPERTIES COMPILE_FLAGS
                                                                   -O0)
endif ()

# Expose necessary symbols for the SymCrypt FIPS module
if (OE_SGX)
  list(
    APPEND
    DEFAULT_VISIBILITY
    malloc.c # for aligned_alloc and free that are pulled in as part of openenclave/corelibc/bits/malloc.h inclusion
    pthread.c # for pthread_mutex_*
    ${MUSLSRC}/string/memcmp.c
    ${MUSLSRC}/string/x86_64/memcpy.s
    ${MUSLSRC}/string/x86_64/memmove.s
    ${MUSLSRC}/string/x86_64/memset.s
    ${MUSLSRC}/stdlib/qsort.c)

  set_property(SOURCE ${DEFAULT_VISIBILITY} APPEND_STRING
               PROPERTY COMPILE_FLAGS " -fvisibility=default")
endif ()

enclave_compile_definitions(oelibc PRIVATE _XOPEN_SOURCE=700)

if (USE_DEBUG_MALLOC)
  enclave_compile_definitions(oelibc PRIVATE OE_USE_DEBUG_MALLOC)
endif ()

if (CODE_COVERAGE)
  # Enable code coverage.
  enclave_compile_options(oelibc PRIVATE -g -O0 -fprofile-arcs -ftest-coverage)
  # Link against libgcov.
  enclave_link_libraries(oelibc PRIVATE gcov)
endif ()

# This project (and its dependents) require the enclave headers and
# library, and the musl headers.
enclave_link_libraries(
  oelibc
  PUBLIC
  oesyscall
  oecore
  oelibc_includes
  oecompiler-rt
  PRIVATE
  oelibasm)

enclave_include_directories(
  oelibc PRIVATE ${PROJECT_BINARY_DIR}/3rdparty/musl/musl/src/include
  ${PROJECT_BINARY_DIR}/3rdparty/musl/musl/src/internal)

if (OE_SGX)
  enclave_include_directories(
    oelibc PRIVATE ${PROJECT_BINARY_DIR}/3rdparty/musl/musl/arch/x86_64)
else ()
  enclave_include_directories(
    oelibc PRIVATE ${PROJECT_BINARY_DIR}/3rdparty/musl/musl/arch/aarch64)
endif ()

set_enclave_property(TARGET oelibc PROPERTY ARCHIVE_OUTPUT_DIRECTORY
                     ${OE_LIBDIR}/openenclave/enclave)

install_enclaves(
  TARGETS
  oelibc
  oelibasm
  EXPORT
  openenclave-targets
  ARCHIVE
  DESTINATION
  ${CMAKE_INSTALL_LIBDIR}/openenclave/enclave)
